#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void dump_int(int *p, int size)
{
    int i = 0;
    for(; i < size; i++) {
        printf("%d->%d\t", i, p[i]);
    }
    printf("\n");
}

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    int len = nums1Size + nums2Size;
    int *p = (int *)calloc(len, sizeof(int));
    int *pp = p;
    int i, j;
    i = j = 0;
    int en = 1;
    int num = *nums2;
    int full = 0;
    printf("num is %d, en %d\n", num, en);
    while(i < nums1Size || j < nums2Size) {

        printf("now en is %d, pp is %d\n", en, *pp);
        if(en) {
            while((full || nums1[i] <= num) && i < nums1Size) {
                *pp++ = nums1[i];
                printf("ii ppp is %d %d\n", i, nums1[i]);
                ++i;
            }
            if(i == nums1Size) full = 1;
            else num = nums1[i];
            printf("num is %d\n", num);
        } else {
            while((full || nums2[j] <= num) && j < nums2Size) {
                *pp++ = nums2[j];
                printf("jj ppp is %d %d\n", j, nums2[j]);
                ++j;
            }
            if(j == nums2Size) full = 1;
            else num = nums2[j];
            printf("num is %d\n", num);
        }
        en = !en;
        dump_int(p, len);
        printf("------i %d j %d -----\n", i, j);
    }
    if(len % 2) {
        return p[len/2];
    }
    return (p[len>>1] + p[(len>>1) - 1]) / 2.0;
}

#define ARR_SIZE(n) (sizeof(n)/sizeof(n[0]))

int arr1[] = {109,182,228,237,271,335,338,344,380,412,512,577,598,606,693,694,729,735,738,755,778,804,848,865,876,907,965,973,977,1076,1086,1099,1178,1198,1218,1226,1329,1349,1373,1384,1510,1527,1535,1556,1612,1679,1719,1728,1752,1808,1815,1818,1949,1980,2001,2102,2209,2249,2308,2320,2325,2340,2355,2414,2464,2490,2525,2526,2562,2660,2665,2674,2680,2715,2758,2774,2777,2786,2836,2853,2883,2933,2994,3001,3024,3083,3137,3247,3266,3291,3347,3355,3365,3436,3485,3502,3548,3557,3573,3630,3643,3649,3706,3753,3766,3798,3802,3806,3821,3864,3948,3998,4017,4047,4110,4112,4130,4142,4188,4244,4247,4268,4271,4314,4345,4381,4441,4442,4446,4458,4459,4472,4526,4606,4777,4821,4835,4860,4900,4910,4916,4921,4937,4986,5091,5094,5103,5122,5129,5135,5163,5171,5186,5190,5261,5287,5293,5372,5419,5442,5444,5462,5495,5503,5542,5574,5594,5701,5730,5751,5826,5919,5958,6074,6200,6258,6344,6350,6391,6397,6417,6463,6473,6507,6510,6512,6572,6598,6606,6647,6671,6687,6717,6719,6766,6828,6897,6907,6921,6932,6937,6962,6994,7027,7068,7079,7088,7135,7137,7192,7232,7275,7278,7302,7324,7357,7392,7437,7481,7493,7552,7558,7639,7745,7784,7814,7818,7821,7825,7887,7888,7920,7934,7935,7986,7989,8001,8079,8121,8123,8126,8141,8149,8183,8217,8227,8273,8288,8291,8298,8406,8466,8561,8640,8733,8751,8787,8858,8868,8871,8910,8923,8961,8986,8991,9061,9082,9122,9125,9131,9182,9237,9255,9278,9278,9331,9369,9386,9404,9435,9451,9455,9459,9476,9512,9523,9557,9666,9752,9757,9893,9896,9896,9988,9995,10007,10017,10044,10087,10099,10107,10107,10132,10161,10191,10197,10217,10219,10276,10296,10328,10370,10428,10437,10452,10462,10478,10506,10526,10536,10553,10662,10668,10758,10758,10793,10799,10804,10852,10884,10952,10991,11053,11064,11079,11080,11085,11087,11111,11124,11143,11182,11185,11208,11229,11306,11309,11423,11479,11493,11510,11516,11530,11578,11684,11699,11700,11753,11840,11858,11898,11921,11986,12008,12013,12041,12144,12146,12148,12165,12230,12261,12265,12327,12341,12346,12384,12434,12441,12449,12460,12497,12569,12583,12708,12737,12812,12891,12900,13005,13054,13107,13109,13245,13253,13287,13322,13327,13332,13344,13388,13406,13455,13477,13501,13585,13585,13641,13647,13690,13692,13702,13723,13761,13765,13841,13892,14009,14037,14086,14125,14168,14246,14249,14271,14342,14358,14390,14502,14560,14574,14588,14673,14688,14703,14714,14714,14737,14786,14798,14825,14832,14840,14870,14885,14891,14927,14956,14958,14975,14992,15005,15008,15068,15096,15114,15131,15184,15213,15223,15401,15410,15418,15435,15476,15547,15601,15613,15691,15720,15856,15860,15911,15919,15946,15989,16038,16133,16150,16153,16193,16302,16310,16354,16410,16432,16434,16453,16482,16504,16507,16543,16578,16607,16675,16676,16677,16784,16857,16864,16885,17006,17017,17052,17167,17178,17183,17185,17190,17243,17262,17322,17334,17357,17362,17408,17424,17430,17556,17573,17586,17649,17665,17693,17727,17791,17814,17819,17827,17862,17921,17937,17956,17982,17983,17994,18039,18073,18097,18119,18132,18145,18155,18173,18201,18203,18203,18253,18276,18291,18361,18384,18443,18466,18516,18528,18547,18550,18620,18738,18772,18787,18838,18860,18879,18885,18892,18903,18933,18956,19076,19088,19089,19090,19100,19121,19160,19204,19251,19266,19286,19309,19398,19442,19472,19474,19479,19529,19567,19568,19574,19591,19597,19609,19639,19666,19757,19789,19843,19845,19920,19929,19942,19997,20037,20076,20155,20199,20206,20208,20210,20212,20254,20257,20264,20310,20376,20414,20420,20441,20469,20494,20503,20522,20592,20594,20712,20743,20745,20799,20802,20824,20851,20875,20908,20909,20932,20976,21049,21104,21214,21254,21285,21293,21313,21397,21411,21433,21483,21520,21530,21551,21696,21699,21797,21808,21823,21894,21912,21951,21957,21987,22040,22070,22104,22163,22176,22231,22254,22259,22273,22293,22296,22327,22341,22372,22378,22379,22439,22444,22448,22477,22485,22490,22507,22542,22565,22610,22634,22769,22792,22808,22810,22819,22821,22869,22910,22943,22981,23023,23041,23054,23055,23191,23203,23220,23231,23236,23251,23295,23313,23341,23475,23531,23532,23539,23549,23585,23646,23653,23666,23699,23738,23744,23759,23811,23816,23907,23918,23919,23976,24098,24110,24169,24185,24198,24254,24335,24489,24519,24534,24548,24550,24594,24613,24637,24650,24698,24708,24714,24731,24743,24749,24751,24752,24796,24812,24831,24859,24863,24930,24950,24957,24957,24998,25008,25018,25051,25086,25132,25218,25281,25338,25339,25370,25401,25551,25562,25600,25646,25673,25700,25705,25750,25813,25846,25847,25889,25906,25908,25916,25917,26003,26006,26038,26051,26053,26072,26077,26089,26121,26124,26138,26194,26197,26203,26249,26253,26274,26305,26310,26379,26409,26496,26514,26552,26567,26569,26591,26642,26696,26700,26724,26730,26759,26791,26875,26907,27065,27101,27108,27110,27130,27145,27146,27148,27207,27281,27316,27376,27419,27432,27435,27442,27453,27465,27478,27495,27518,27563,27569,27580,27588,27595,27642,27661,27667,27693,27781,27801,27826,27843,27856,27887,27918,27920,27941,27966,27980,27990,28014,28051,28063,28317,28359,28388,28392,28415,28468,28521,28522,28574,28631,28633,28635,28649,28701,28703,28730,28737,28821,28841,28876,28886,28889,28902,29056,29059,29075,29123,29157,29169,29171,29221,29223,29249,29252,29278,29282,29374,29384,29471,29494,29701,29711,29713,29729,29761,29802,29814,29820,29872,29874,29894,29939,29987,30125,30129,30139,30182,30192,30201,30235,30242,30271,30284,30287,30297,30315,30362,30376,30427,30484,30607,30611,30746,30755,30843,30852,30883,30903,30968,30998,31019,31021,31037,31094,31143,31326,31369,31390,31463,31521,31555,31582,31600,31600,31619,31626,31637,31706,31749,31786,31793,31858,31894,31929,31961,31969,31976,32029,32031,32087,32123,32135,32195,32306,32309,32392,32402,32405,32413,32424,32454,32464,32510,32571,32586,32679,32701};
int arr[] = {7,43,81,97,126,128,156,162,219,284,396,402,404,432,453,458,490,519,559,613,629,648,669,684,689,712,714,749,766,771,792,792,803,822,846,922,966,1079,1080,1115,1123,1153,1186,1253,1310,1338,1389,1417,1421,1460,1480,1565,1765,1780,1798,1840,1854,1927,1953,1965,1972,1993,2009,2010,2024,2096,2139,2177,2286,2298,2303,2400,2445,2488,2503,2531,2534,2555,2575,2586,2649,2657,2704,2754,2815,2821,2830,2859,2868,2914,2915,2919,2933,2941,3012,3058,3063,3068,3113,3124,3140,3310,3358,3400,3403,3414,3449,3478,3485,3512,3518,3545,3600,3757,3758,3766,3781,3797,3883,3888,3908,3932,3989,4065,4118,4121,4122,4125,4167,4172,4179,4183,4190,4195,4198,4207,4266,4269,4276,4277,4304,4363,4369,4399,4437,4480,4519,4541,4541,4631,4640,4693,4819,4902,4906,4947,4966,4968,4976,4990,4994,5000,5003,5009,5019,5036,5206,5208,5397,5472,5494,5537,5549,5603,5606,5609,5624,5655,5671,5688,5703,5766,5778,5806,5809,5809,5817,5834,5871,5885,6030,6031,6034,6086,6128,6136,6160,6182,6253,6254,6286,6288,6290,6291,6347,6356,6366,6397,6479,6518,6535,6546,6598,6631,6714,6740,6769,6823,6824,6862,6898,7035,7106,7157,7309,7311,7318,7411,7412,7427,7524,7533,7604,7605,7612,7613,7620,7758,7815,7844,7847,7915,7948,7974,8024,8028,8046,8086,8120,8127,8145,8193,8199,8205,8209,8214,8353,8359,8364,8377,8380,8417,8434,8541,8558,8616,8623,8628,8642,8671,8689,8705,8720,8852,8917,8953,8969,8987,9032,9033,9132,9150,9261,9263,9333,9343,9401,9449,9539,9702,9796,9845,9856,9869,9908,9933,9955,9959,9982,9988,9992,10016,10039,10066,10077,10088,10124,10149,10161,10200,10238,10240,10247,10284,10294,10319,10347,10385,10395,10426,10460,10495,10504,10517,10538,10555,10660,10663,10673,10728,10728,10735,10784,10859,10941,10986,10994,10999,11045,11051,11071,11144,11149,11195,11196,11251,11279,11356,11369,11369,11370,11456,11490,11564,11692,11718,11721,11730,11752,11796,11833,11849,11862,11917,11925,11930,11977,12036,12058,12160,12269,12309,12318,12324,12352,12417,12499,12550,12589,12598,12601,12723,12836,12870,12891,13037,13052,13076,13103,13111,13201,13207,13261,13265,13276,13287,13372,13412,13442,13500,13567,13574,13634,13648,13679,13710,13732,13778,13789,13823,13871,13882,13885,13896,13912,13929,13956,13957,13998,14095,14096,14121,14199,14205,14226,14231,14232,14245,14246,14254,14275,14313,14411,14417,14425,14465,14537,14550,14555,14575,14607,14627,14641,14653,14703,14707,14736,14754,14762,14787,14823,14835,14840,14892,14895,14952,15033,15070,15092,15104,15112,15154,15214,15244,15258,15375,15409,15447,15502,15533,15623,15680,15746,15749,15770,15778,15781,15911,15923,15933,15976,16016,16017,16083,16201,16212,16239,16274,16333,16352,16367,16406,16434,16451,16452,16507,16526,16559,16591,16617,16648,16652,16654,16664,16763,16768,16819,16910,16921,16922,16935,16978,17033,17132,17189,17209,17265,17276,17313,17319,17347,17369,17392,17395,17407,17423,17434,17465,17487,17501,17523,17524,17549,17576,17585,17587,17663,17669,17741,17754,17765,17850,17881,17891,17906,17921,17922,17939,17939,17990,18014,18021,18048,18059,18075,18194,18212,18221,18235,18289,18341,18350,18447,18463,18482,18486,18489,18517,18522,18607,18629,18630,18637,18647,18647,18738,18808,18846,18856,18906,18975,18982,19005,19065,19069,19078,19131,19137,19161,19206,19262,19281,19323,19350,19378,19440,19455,19479,19501,19528,19563,19617,19634,19647,19658,19767,19769,19833,19905,19964,19986,19997,20053,20094,20113,20195,20213,20234,20256,20256,20317,20349,20405,20414,20455,20544,20545,20547,20547,20551,20617,20697,20894,20928,20940,20944,21159,21270,21278,21290,21359,21387,21418,21490,21514,21529,21549,21594,21616,21643,21660,21686,21701,21817,21823,21836,21872,21893,21898,21907,21928,21942,21944,21982,22008,22058,22137,22157,22171,22179,22210,22228,22248,22278,22300,22315,22334,22469,22481,22497,22512,22566,22628,22645,22651,22776,22817,22821,22852,22859,22867,22876,22888,22907,23002,23030,23037,23081,23087,23160,23173,23180,23187,23248,23279,23393,23404,23436,23481,23536,23539,23620,23626,23633,23679,23716,23733,23744,23745,23926,23976,24019,24024,24027,24029,24045,24059,24097,24099,24113,24116,24172,24179,24248,24253,24326,24377,24439,24482,24492,24570,24738,24744,24763,24812,24835,24868,24917,24932,24934,24942,24947,24989,25035,25054,25112,25135,25183,25233,25259,25270,25271,25384,25407,25424,25461,25508,25511,25523,25587,25611,25628,25654,25658,25672,25673,25688,25749,25840,25842,25843,25922,25950,25965,25977,26094,26137,26169,26196,26225,26293,26312,26335,26371,26382,26410,26417,26437,26456,26460,26468,26479,26495,26498,26503,26583,26623,26634,26730,26761,26772,26833,26843,26893,26941,26946,27095,27099,27103,27105,27105,27191,27202,27222,27247,27382,27394,27402,27405,27429,27429,27514,27559,27563,27564,27574,27576,27598,27622,27714,27756,27757,27796,27832,28002,28011,28026,28071,28101,28138,28142,28164,28199,28217,28227,28247,28250,28267,28284,28289,28299,28350,28374,28382,28389,28421,28443,28476,28558,28560,28561,28566,28583,28585,28602,28618,28644,28689,28714,28717,28759,28827,28845,28885,28933,28983,29074,29076,29076,29132,29137,29147,29171,29206,29245,29301,29416,29527,29552,29595,29640,29647,29716,29720,29749,29768,29777,29786,29796,29804,29808,29828,29840,29888,29913,29927,29945,30023,30024,30046,30064,30113,30146,30173,30180,30193,30225,30237,30265,30327,30394,30433,30437,30488,30545,30545,30560,30581,30587,30588,30609,30634,30644,30661,30713,30728,30730,30743,30749,30750,30754,30804,30820,30824,30891,30895,30897,30918,30943,30944,31000,31084,31116,31227,31282,31353,31653,31664,31665,31693,31707,31774,31796,31806,31881,31908,31957,31969,31988,32035,32039,32101,32134,32162,32222,32236,32244,32302,32308,32312,32413,32446,32496,32631,32654,32659,32662,32723,32746,32751};

int main(int argc, char *argv[])
{
    int a[2] = {1};
    int b[2] = {1};
    double ret = findMedianSortedArrays(arr, ARR_SIZE(arr), arr1, ARR_SIZE(arr1));
    printf("ret is %f\n", ret);
    return 0;
}
